/**
 * Copyright (c) 2016-2020 https://github.com/zhaohuatai
 *
 * contact 824069438@qq.com
 *  
 */
package zfes.snowier.projects.sample.biz.service.impl;

import java.util.Date;
import java.util.Map;
import java.util.Optional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.core.data.DataSet;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZObjectUtil;
import org.zfes.snowier.core.util.ZStrUtil;

import zfes.snowier.projects.sample.biz.dao.SampleMapper;
import zfes.snowier.projects.sample.biz.model.Sample;
import zfes.snowier.projects.sample.biz.service.ISampleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
@Service
public class SampleServiceImpl implements ISampleService{
 	@Autowired
	private SampleMapper sampleMapper;
 	
 
 	@Cacheable
 	@Override
 	public Optional<String>   loadCodeById(Long id){
 		if(!ZObjectUtil.isEmpty(id)){
 			return Optional.ofNullable(sampleMapper.selectCodeById(id));
 		}
		 return Optional.empty();
	 }
	 
	 
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<Sample>  loadOrgById(Long id) {
		if(id!=null){
			return Optional.ofNullable(sampleMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<Sample> loadOrgByCode(String code) {
		if(ZStrUtil.hasText(code)){
			return Optional.ofNullable(sampleMapper.selectByCode(code));
		}
		return Optional.empty();
	}
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createSample(Sample sysOrg) {
		ZBeanUtil.validateBean(sysOrg);
		Optional<Sample> orgOp=loadOrgByCode(sysOrg.getCode());
		if(orgOp.isPresent()){
			ZAlert.Error("该编码已经存在");
		}
		sysOrg.setCreateTime(new Date());
		sampleMapper.insertSelective(sysOrg);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateSample(Sample sample) {
		ZBeanUtil.validateBean(sample);
		Sample samplex=sampleMapper.selectByCode(sample.getCode());
		if(!ZObjectUtil.isEmpty(samplex) && (!sample.getId().equals(samplex.getId()))){
			ZAlert.Error("该编码已经存在");
		}
		sampleMapper.updateByPrimaryKeySelective(sample);
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		pm.getStr("name").ifPresent(v->pm.updateParam( "name", "%"+v+"%"));
		pm.getStr("orgTreeCode").ifPresent(v->pm.updateParam( "orgTreeCode", ""+v+"%"));
		DataSet ds=DataSet.newDS2(sampleMapper.selectListCount(pm), sampleMapper.selectList(pm));
		return ds;
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setOrgStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择数据");
		ZAssert.notNull(status, "状态参数错误");
		sampleMapper.updateStatus(id,status);
	}


}